package com.zhongwang.mms.module.buy.service;

import com.zhongwang.mms.module.basic.dao.VerifyProcessMapper;
import com.zhongwang.mms.module.basic.model.VerifyProcess;
import com.zhongwang.mms.module.buy.dao.BPreOrderPlanDao;
import com.zhongwang.mms.module.buy.model.BPreOrderPlan;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.zhongwang.mms.constant.Constant.*;

@SuppressWarnings("ALL")
@Service
@Log4j2
public class BuyPreOrderPlanCheckService {
    @Autowired
    private BPreOrderPlanDao bPreOrderPlanDao;
    @Autowired
    private VerifyProcessMapper verifyProcessMapper;
    @Autowired
    private LogService  logService;

    /**
     * 获取审核列表
     * @param where
     * @return
     */
    public List<BPreOrderPlan> findBPreOrderPlanByQuery(BPreOrderPlan where) {
        where.setVerifier(ShiroUtils.getUserId());
        return bPreOrderPlanDao.selectListWithReportCheck(where);
    }

    /**
     * 根据预购单好获取有狗计划列表
     * @param code
     * @return
     */
    public List<BPreOrderPlan> findBPreOrderPlanByCode(String code) {
        return bPreOrderPlanDao.selectListByCode(code);
    }
    /**
     * 根据主键获取对应的预购计划数据
     * @param id 主键
     * @return 对应的预购计划数据
     */
    public BPreOrderPlan getBPreOrderPlanByPrimaryKey(Integer id) {
        return bPreOrderPlanDao.selectByPrimaryKey(id);
    }

    /**
     * 审核通过
     * @param codes 审批通过的预购编码集合
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer verify(List<String> codes) {
        Integer result = 0;
        for (String code : codes) {
            VerifyProcess vp = verifyProcessMapper.findCurVpBySheetCode(code);
            if (vp.getComp() == 1) {
                // 如果是最后一步审核， 修改计划单状态， 添加或更新reportResult表
                // 修改，添加一个确认流程，后来又取消了
                List<BPreOrderPlan> bPreOrderPlanList = bPreOrderPlanDao.selectListByCode(code);
                for (BPreOrderPlan bPreOrderPlan : bPreOrderPlanList) {
                    bPreOrderPlan.setStatus(FORM_ALL_APPROVED);
                    result += bPreOrderPlanDao.updatePreOrderStatus(bPreOrderPlan);
                }
            } else {
                // 修改下一个审核节点为当前节点
                VerifyProcess nextVp = verifyProcessMapper.findNextVp(vp);
                nextVp.setCur(1);
                result += verifyProcessMapper.updateCurById(nextVp.getId(),nextVp.getCur());
            }
            logService.saveLog("采购管理-预购计划审核", "审核通过预购计划，预购编号为：" + vp.getSheetCode());
            result += verifyProcessMapper.updateStatusAndCurAndTimeById(vp.getId(), 2, 0, new Date());
        }
        return result;
    }

    /**
     * 驳回
     * @param codes 驳回的预购编码集合
     * @param rejectReason 驳回原因
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer reject(List<String> codes, String rejectReason) {
        Integer result = 0;
        for (String code : codes) {
            VerifyProcess vp = verifyProcessMapper.findCurVpBySheetCode(code);
            logService.saveLog("采购管理-预购计划审核", "驳回预购计划，预购编号为：" + vp.getSheetCode());
            List<BPreOrderPlan> bPreOrderPlanList = bPreOrderPlanDao.selectListByCode(code);
            for (BPreOrderPlan bPreOrderPlan : bPreOrderPlanList) {
                if(rejectReason.trim().equals("")){
                    bPreOrderPlan.setRejectReason("被驳回");
                }else{
                    bPreOrderPlan.setRejectReason(rejectReason);
                }
                bPreOrderPlan.setStatus(FORM_STATUS_NOT_SUBMITTED);
                result += bPreOrderPlanDao.updatePreOrder(bPreOrderPlan);
            }
            //直接驳回到提交前
            result += verifyProcessMapper.delBySheetCode(code);
        }
        return result;
    }
}
